package com.mc.web;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import com.mc.model.Major;
import com.mc.model.Path;
import com.mc.model.Teacher;
import com.mc.model.Teachertype;
import com.mc.model.Teteacherytpe;
import com.mc.model.User;
import com.mc.service.MajorService;
import com.mc.service.TeacherService;
import com.mc.service.TeachertypeService;
import com.mc.service.TeteachertypeService;
import com.mc.utils.PathUtil;

@Controller
@RequestMapping("/backend")
public class TeacherAction {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(TeacherAction.class);
   
	@Autowired private TeachertypeService teachertypeService;
	@Autowired private TeteachertypeService teteachertypeService;
	@Autowired private TeacherService teacherService;
	@Autowired private MajorService majorService;
	
	private Path path = null;
	@RequestMapping("/addteacherui")
	public String addTeacherui(ModelMap modelMap)
	{
		List<Major> majors = majorService.selectAllMajors();
		List<Teachertype> teachertypes = teachertypeService.selectAllTtypes();
		//logger.info(teachertypes.toString());
		modelMap.addAttribute("teachertypes", teachertypes);
		
		modelMap.addAttribute("majors",majors);
		path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:addteacher","ViewTitle:添加老师","smenu:teachermag","mmenu:addteachers"});		
        
		return PathUtil.returnStr(path, modelMap);
	}
	
	
	@RequestMapping("/addteacher")
	public String addTeacher(@RequestParam("belong")int belong,ModelMap modelMap,Teacher teacher,@RequestParam("major1")int majorid,HttpSession session)
	{
		
	//	if(typeid == 0)
		if(majorid!=0)
		{
			Major major = majorService.selectByPrimaryKey(majorid);
			teacher.setMajorId(major);
			//logger.info(teacher.toString());
			
				teacherService.insertTeacher(teacher.getName(), teacher.getNo(), teacher.getGender(),
						major.getId(), teacher.getPhone(), teacher.getQq(), belong,
						((User)session.getAttribute("user")).getId(),teacher.getIntroduce());
			
		}else{
			teacherService.insertTeacher(teacher.getName(), teacher.getNo(), teacher.getGender(),
					0, teacher.getPhone(), teacher.getQq(), belong,
					((User)session.getAttribute("user")).getId(),teacher.getIntroduce() );
		}
		modelMap.addAttribute("message",true);
		return "forward:/backend/addteacherui";
	}
	@RequestMapping("/teachermagui")
	public String teachermag( ModelMap modelMap)
	{
		List<Teteacherytpe> steachers = teteachertypeService.selectAllsteacher();
		modelMap.addAttribute("steachers", steachers);
		
		List<Teteacherytpe> yxteachers = teteachertypeService.selectAllyxteacher();
		modelMap.addAttribute("yxteachers", yxteachers);
		
		List<Teacher> cteachers = teacherService.selectAllcteachers();
		modelMap.addAttribute("cteachers", cteachers);
		
		List<Teacher> rteachers = teacherService.selectAllrteachers();
		modelMap.addAttribute("rteachers", rteachers);
		
		List<Teacher> xteachers = teacherService.selectAllxteachers();
		modelMap.addAttribute("xteachers", xteachers);
		
		List<Teacher> yteachers = teacherService.selectAllyteachers();
		modelMap.addAttribute("yteachers", yteachers);
		
		List<Teacher> wteachers = teacherService.selectAllwteachers();
		modelMap.addAttribute("wteachers", wteachers);
		
       path = PathUtil.setPathParams(new String[]{"PackageName:backend","ViewName:shiziduiwu","ViewTitle:教师管理","smenu:teachermag","mmenu:teachermag"});		
        
		return PathUtil.returnStr(path, modelMap);
	}
}
